#include "vlbrushcontextwidget.h"

VLBrushContextWidget::VLBrushContextWidget(QWidget *parent) :
    VLContextWidget(parent)
{
    layout = setupLayout();
    nameLabel = createAndAddLabel(layout, tr("BRUSH"));

    shapeLabel = createAndAddLabel(layout, tr("Shape"));
    shapesList = new QStringList();
    *shapesList << "Cube" << "Cuboid" << "Sphere" << "Pyramid";
    shapeBox = createAndAddComboBox(layout, shapesList);
    connect(shapeBox, SIGNAL(activated(int)), this, SLOT(setShape(int)));

    sizeLabel = createAndAddLabel(layout, tr("Size"));
    sizeBox = createAndAddSpinBox(layout, 1, 100);
    connect(sizeBox, SIGNAL(valueChanged(int)), this, SLOT(setSize(int)));

    breadthLabel = createAndAddLabel(layout, tr("Breadth"));
    breadthBox = createAndAddSpinBox(layout, 1, 100);
    connect(breadthBox, SIGNAL(valueChanged(int)), this, SLOT(setBreadth(int)));

    heightLabel = createAndAddLabel(layout, tr("Height"));
    heightBox = createAndAddSpinBox(layout, 1, 100);
    connect(heightBox, SIGNAL(valueChanged(int)), this, SLOT(setHeight(int)));

    addShapeBtn = createAndAddButton(layout, tr("Add Shape"));
    connect(addShapeBtn, SIGNAL(clicked()), this, SLOT(addShape()));

    delShapeBtn = createAndAddButton(layout, tr("Delete Shape"));
    connect(delShapeBtn, SIGNAL(clicked()), this, SLOT(delShape()));

    setDefaults();
}

void VLBrushContextWidget::setShape(int shape) {
    switch (shape) {
        case CUBE:
            breadthBox->setDisabled(true);
            heightBox->setDisabled(true);
            delShapeBtn->setDisabled(true);
            break;
        case CUBOID:
            breadthBox->setDisabled(false);
            heightBox->setDisabled(false);
            delShapeBtn->setDisabled(true);
            break;
        case SPHERE:
            breadthBox->setDisabled(true);
            heightBox->setDisabled(true);
            delShapeBtn->setDisabled(true);
            break;
        case PYRAMID:
            breadthBox->setDisabled(true);
            heightBox->setDisabled(false);
            delShapeBtn->setDisabled(true);
            break;
        default:
            sizeBox->setDisabled(true);
            breadthBox->setDisabled(true);
            heightBox->setDisabled(true);
            delShapeBtn->setDisabled(false);
            break;
    }

    emit shapeChanged(shape);
}

void VLBrushContextWidget::setSize(int size) {
    emit sizeChanged(size);
}

void VLBrushContextWidget::setHeight(int height) {
    emit heightChanged(height);
}

void VLBrushContextWidget::setBreadth(int breadth) {
    emit breadthChanged(breadth);
}

void VLBrushContextWidget::addShape() {
    QString input = QInputDialog::getText(this, tr("Custom Shape Name"),
                                          tr("New Shape Name"));
    shapesList->append(input);
    shapeBox->addItem(input);
    int index = shapeBox->count() - 1;
    emit shapeAdded(input, index);
}

void VLBrushContextWidget::delShape() {
    int index = shapeBox->currentIndex();
    shapeBox->setCurrentIndex(index - 1);
    setShape(index - 1);
    shapeBox->removeItem(index);
    shapesList->removeAt(index);
    emit shapeDeleted(index);
}

void VLBrushContextWidget::setDefaults() {
    int shape = TOOL_BRUSH->getShape() - 1;
    int size = TOOL_BRUSH->getSize();
    int height = TOOL_BRUSH->getHeight();
    int breadth = TOOL_BRUSH->getBreadth();

    sizeBox->setValue(size);
    heightBox->setValue(height);
    breadthBox->setValue(breadth);
    shapeBox->setCurrentIndex(shape);
    setShape(shape);
}

QPushButton * VLBrushContextWidget::createAndAddButton(QHBoxLayout *tlayout, QString text) {
    QPushButton *btn = new QPushButton(text);
    tlayout->addWidget(btn);
    return btn;
}
